$(function () {
    $("#topBtn").click(setTop);
    $("#wonderfulBtn").click(setWonderful);
    $("#deleteBtn").click(setDelete);
})

function like(btn,entityType,entityId,entityUserId,postId) {
    $.post(
        //发送请求的地址
        CONTEXT_PATH + "/like",
        //向服务端发送的请求参数
        {"entityType":entityType,"entityId":entityId,"entityUserId":entityUserId,"postId":postId},
        //data是发送成功的回调函数，也就是服务端返回的json字符串
        function (data) {
            //将服务端返回的json字符串转换为json对象，json字符串中包含了服务端传给前端的值
            data = $.parseJSON(data);
            if (data.code == 0){
                $(btn).children("i").text(data.likeCount);
                $(btn).children("b").text(data.likeStatus == 1 ? '已赞' : '赞');
            }else{
                alert(data.msg);
            }
        }
    )
}

function setTop() {
    $.post(
        CONTEXT_PATH + "/discuss/top",
        {"id":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code == 0){
                $("#topBtn").attr("disabled","disabled");
            }else{
                alert(data.msg);
            }
        }
    )
}

function setWonderful() {
    $.post(
        CONTEXT_PATH + "/discuss/wonderful",
        {"id":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code == 0){
                $("#wonderfulBtn").attr("disabled","disabled");
            }else{
                alert(data.msg);
            }
        }
    )
}


function setDelete() {
    $.post(
        CONTEXT_PATH + "/discuss/delete",
        {"id":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code == 0){
               location.href = CONTEXT_PATH + "/index";
            }else{
                alert(data.msg);
            }
        }
    )
}